class SampleController < ApplicationController
  def chart_data
    xml = "<chart palette='1' caption='SET Index' subCaption='' scrollToEnd='1' connectNullData='0' formatNumber='1' formatNumberScale='0' numdivlines='9' lineThickness='2' showValues='0' anchorRadius='3' anchorBgAlpha='50' showAlternateVGridColor='1' numVisiblePlot='120' animation='0'>"
    xml += "<categories>"
    trades = StockTrade.find(:all, :conditions=>["stock_id=?", 1], :order=>"trade_date")
    old_date = nil
    trades.each do |t|
      trade_date_str = "#{t.trade_date.day}/#{t.trade_date.month}/#{t.trade_date.year+543}"
      if old_date
        date = t.trade_date
        if date.month==old_date.month
          xml += "<category label='#{trade_date_str}' showLabel='0'/>"
        else
          xml += "<category label='#{trade_date_str}'/>"
        end
        old_date = t.trade_date
      else
        xml += "<category label='#{trade_date_str}'/>"
        old_date = t.trade_date
      end
    end
    xml += "</categories>"
    xml += "<dataset seriesName='SET' color='202080' anchorRadius='2'>"
    trades.each do |t|
      ev = "#{t.trade_date.day}/#{t.trade_date.month}/#{t.trade_date.year+543}"
#      ev = 'xxx'
      xml += "<set value='#{t.price}' link='j-fillev-#{ev}'/>"
    end
    xml += "</dataset>"
    xml += "</chart>"
    render :text=>xml
  end

  def chart_data2
    xml = "<chart compactDataMode='1' dataSeparator='|' palette='3' caption='SET Index' subCaption='' connectNullData='0' formatNumber='1' formatNumberScale='0'>"
    xml += "<categories>"
    trades = StockTrade.find(:all, :conditions=>["stock_id=?", 1], :order=>"trade_date")
    date_array = trades.map{|t| "#{t.trade_date.day}/#{t.trade_date.month}/#{t.trade_date.year+543}"}
    xml += date_array.join('|')
    xml += "</categories>"
    xml += "<dataset seriesName='SET'>"
    value_array = trades.map{|t| "#{t.price}"}
    xml += value_array.join('|')
    xml += "</dataset>"
    xml += "</chart>"
    render :text=>xml
  end

end
